package frontend;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import logs.LogFactory;

import org.apache.log4j.Logger;

import backend.InputValidation;
import backend.db.ORM;

/**
 * Servlet implementation class EditUserAccount
 */
public class EditUserAccount extends HttpServlet {
  private static final Logger logger = LogFactory.getLog(EditUserAccount.class);
  private static final long serialVersionUID = 1L;
  
  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
   *      response)
   */
  @Override protected void doPost(final HttpServletRequest request, final HttpServletResponse response) {
    logger.debug("EditUserAccount");
    final String username = request.getParameter("username");
    final String action = request.getParameter("action");
    final String value = request.getParameter("newValue");
    if (!InputValidation.validateEditUserAccount(username, action, value)) {
      logger.debug("invalid user details");
      response.setStatus(HttpStatus.SC_OK + 1);
      return;
    }
    ORM.updateUserField(username, action, value);
    logger.debug("user updated");
  }
}
